﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;
using WeifenLuo.WinFormsUI.Docking;
using Xeno.Core;
using Xeno.Module;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;

namespace XenoStudio

{
    public partial class SceneEditor : DockContent
    {
        public static XenoGraph EditGraph { get; set; }
        public static XenoNode EditCam { get; set; }
        public static XenoNode EditTarget { get; set; }
        public static XenoNode AppCam { get; set; }
        public static XenoNode AppGraph { get; set; }
     
        public SceneEditor()
        {
            InitializeComponent();
        }

        private void glControl1_Resize(object sender, EventArgs e)
        {
           
        }

        private void glControl1_Load(object sender, EventArgs e)
        {
            EditGraph = new XenoGraph("EditorGraph1");
            EditCam = XenoFactory.Node.Camera("EditorCamera1", EditGraph.RootNode);
            glControl1.Invalidate();
            CreateGrid(1000,350);
            ResetEditCam();
            XenoNode tmpLight = XenoFactory.Light.Directional("LightTemp001", EditGraph.RootNode);
        }
        public void ResetEditCam()
        {
            EditCam.LocalPosition = new Vector3(0, 10, 10);
            EditCam.LookAt(new Vector3(0, 0, 0));
        }
        public void CreateGrid(float size,float texScale)
        {
       
            XenoNode grid = XenoFactory.Node.Mesh("Grid001", EditGraph.RootNode);

            XenoVertex[] v = new XenoVertex[4];
            v[0] = new XenoVertex(new Vector3(-size, 0, -size), new Vector3(0, 0, 0));
            v[1] = new XenoVertex(new Vector3(size, 0, -size), new Vector3(texScale, 0, 0));
            v[2] = new XenoVertex(new Vector3(size, 0, size), new Vector3(texScale, texScale, 0));
            v[3] = new XenoVertex(new Vector3(-size, 0, size), new Vector3(0, texScale, 0));

            XenoTriangle[] t = new XenoTriangle[4];
            t[0] = new XenoTriangle(0, 1, 2);
            t[1] = new XenoTriangle(2, 3, 0);
            t[2] = new XenoTriangle(0, 2, 1);
            t[3] = new XenoTriangle(2, 0, 3);

            grid.Mesh.AddVertices = v;

            grid.Mesh.AddTriangles = t;

            var mat = new XenoMaterial("GridMaterial001");

            mat.DiffuseMap = new XenoTexture2D("Xeno/Gfx/Editor/GridNeon1.png");

            mat.Effect = new XenoEffect("", "Xeno/Shader/GridShader.glsl");

            grid.MeshRenderer.Material = mat;

            grid.Mesh.UpdateNormals();
            grid.MeshRenderer.NoLighting = true;



        }
        public static class Edit
        {
            public static float CamPitch = 0.0f;
            public static float CamYaw = 0.0f;
            public static float CamRoll = 0.0f;
            public static bool CamUse = false;
      
        }
        private void glControl1_Paint(object sender, PaintEventArgs e)
        {
            glControl1.MakeCurrent();
            // Environment.Exit(-1);
            XenoCore.CurrentView = EditCam;
            XenoCore.DisplayWidth = glControl1.Width;
            XenoCore.DisplayHeight = glControl1.Height;
            GL.Viewport(0, 0, glControl1.Width, glControl1.Height);

            EditCam.Camera.Projection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(EditCam.Camera.FieldOfView), (float)glControl1.Width / (float)glControl1.Height, 0.1f, 1000.0f);


            GL.ClearColor(Color.DarkSlateBlue);


            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            XenoCore.LightList = EditGraph.RootNode.AllWithType(typeof(XenoMod_Light), null);
            EditGraph.RenderGraph();

            glControl1.SwapBuffers();
        }

        private void glControl1_MouseDown(object sender, MouseEventArgs e)
        {

        }

        private void glControl1_MouseUp(object sender, MouseEventArgs e)
        {
         
        }
        public static int MouseX=0, MouseY=0;
        public static int MouseXI = 0, MouseYI = 0;
        private void glControl1_MouseMove(object sender, MouseEventArgs e)
        {

            MouseXI = e.X - MouseX;
            MouseYI = e.Y - MouseY;
            MouseX = e.X;
            MouseY = e.Y;

            if (Edit.CamUse)
            {
                Edit.CamPitch += MouseYI;
                Edit.CamYaw += MouseXI;
                EditCam.Rotate(Edit.CamPitch, Edit.CamYaw, Edit.CamRoll);
            }
            
        }

        private void UpdateCamera_Tick(object sender, EventArgs e)
        {
          
        }

        private void glControl1_MouseUp_1(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                Edit.CamUse = false;
            }
        }

        private void glControl1_MouseDown_1(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                Edit.CamUse = true;
            }
        }

        private void glControl1_Move(object sender, EventArgs e)
        {

        }

        private void glControl1_MouseMove_1(object sender, MouseEventArgs e)
        {
            MouseXI = e.X - MouseX;
            MouseYI = e.Y - MouseY;
            MouseX = e.X;
            MouseY = e.Y;

            if (Edit.CamUse)
            {
                Edit.CamPitch -= MouseYI*0.2f;
                Edit.CamYaw -= MouseXI*0.2f;
                EditCam.Rotate(Edit.CamPitch, Edit.CamYaw, Edit.CamRoll);
                glControl1.Invalidate();
            }
        }
    }
}
